package com.skyable.third.controller;

import com.alibaba.fastjson.JSONObject;
import com.skyable.common.constants.param.CommonParams;
import com.skyable.common.common.ResponseResult;
import com.skyable.common.constants.exception.ThirdExcpConstants;
import com.skyable.common.utils.ParamUtil;
import com.skyable.third.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author GaoYuan
 * @title: SmsController
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1314:35
 */
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsUtil smsUtil;

    @Value("${server.port}")
    private String serverPort;
    @GetMapping("test")
    public ResponseResult test() {
        return ResponseResult.success(serverPort);
    }

    /**
     * 发送验证码
     * @param params
     * @return
     */
    @PostMapping("/send/code")
    public ResponseResult sendCode(@RequestBody Map<String,String> params) {
        ParamUtil.checkParams(params,"phone","code");

        String phone = params.get("phone");
        String code = params.get("code");
        JSONObject jsonObject = smsUtil.sendCode(phone, code);
        if(jsonObject.containsKey(CommonParams.CODE)) {
            if(jsonObject.getInteger(CommonParams.CODE) == 0) {
                return ResponseResult.success(CommonParams.SMS_MSG);
            }
        }
        return ResponseResult.error(ThirdExcpConstants.ERROR_CODE_SEND_FAILD,ThirdExcpConstants.ERROR_MSG_SEND_FAILD);
    }

    /**
     * 发送用户密码
     * @param params
     * @return
     */
    @PostMapping("/send/pass")
    public ResponseResult sendPass(@RequestBody Map<String,String> params) {
        ParamUtil.checkParams(params,"phone","pass","userName");

        String phone = params.get("phone");
        String pass = params.get("pass");
        String userName = params.get("userName");
        JSONObject jsonObject = smsUtil.sendPass(phone, userName + "," + pass);
        if(jsonObject.containsKey(CommonParams.CODE)) {
            if(jsonObject.getInteger(CommonParams.CODE) == 0) {
                return ResponseResult.success(CommonParams.SMS_MSG);
            }
        }
        return ResponseResult.error(ThirdExcpConstants.ERROR_CODE_SEND_FAILD,ThirdExcpConstants.ERROR_MSG_SEND_FAILD);
    }

    /**
     * 发送失败原因
     * @param params
     * @return
     */
    @PostMapping("/send/deny")
    public ResponseResult sendDeny(@RequestBody Map<String,String> params) {
        ParamUtil.checkParams(params,"phone","reason");

        String phone = params.get("phone");
        String reason = params.get("reason");
        JSONObject jsonObject = smsUtil.sendFail(phone, reason);
        if(jsonObject.containsKey(CommonParams.CODE)) {
            if(jsonObject.getInteger(CommonParams.CODE) == 0) {
                return ResponseResult.success(CommonParams.SMS_MSG);
            }
        }
        return ResponseResult.error(ThirdExcpConstants.ERROR_CODE_SEND_FAILD,ThirdExcpConstants.ERROR_MSG_SEND_FAILD);
    }
}
